package cc.fs.zhihu.ui.fragment

import android.app.Fragment
import android.os.Bundle
import android.provider.MediaStore
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cc.fs.zhihu.R
import cc.fs.zhihu.beans.FileBean
import cc.fs.zhihu.beans.VideoBean1
import cc.fs.zhihu.ui.MainActivity
import cc.fs.zhihu.ui.adapter.SelectFileAdapter
import cc.fs.zhihuapp.utils.bindView
import java.io.File
import java.util.*

/**
 * Created by fostion on 2015/10/13.
 */
class FavFileFragment : Fragment() {

    val recycleView: RecyclerView by bindView(R.id.recycleview)
    val files: ArrayList<FileBean> = ArrayList()
    val videos: ArrayList<VideoBean1> = ArrayList()
    var adapter:SelectFileAdapter ?= null
    val parent:MainActivity by lazy {
        activity as MainActivity
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater?.inflate(R.layout.fragment_fav_file,container,false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //get data
        getFiles()

        adapter = SelectFileAdapter(files)
        recycleView.adapter = adapter
        recycleView.layoutManager = GridLayoutManager(activity,3)

        adapter?.onClick = {
            file ->
            if(file.category!!.equals("dir")){
                parent.selectFile(file?.path ?: "")
            }
        }

    }

    fun getFiles(){
        getVideo()
        for(video in videos){
            val file = File(video.path).parentFile
            if(!findVideoFile(file.absolutePath)){
                val fileBean = FileBean()
                fileBean.name = file.name
                fileBean.path = file.absolutePath
                fileBean.category = "dir"
                files.add(fileBean)
            }
        }
    }

    fun getVideo(){
        val cursor = activity.contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,null, null)

        cursor.moveToFirst()
        while (!cursor.isAfterLast) {
            val video = VideoBean1()
            video.id = ""+cursor.getInt(0)//cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.BUCKET_ID))
            video.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE))
            video.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA))
            video.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.MIME_TYPE))
            video.duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DURATION))
            video.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE))
            //get thumb
            val section = MediaStore.Video.Thumbnails.VIDEO_ID +"=" + video.id
            val thumbCursor = activity.contentResolver.query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, null, section,null, null)
            if(thumbCursor.moveToFirst()){
                video.imgPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA))
            }
            videos.add(video)
            cursor.moveToNext()
        }
    }

    fun findVideoFile(path:String):Boolean{
        val result = false;
        for(file in files){
            if(file.path!!.equals(path)){
                return true
            }
        }
        return  result;
    }

}